package com.smforj.ssm.mina.message;

import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;

/***
 * 消息信息包含消息头与消息体
 * @author ghj
 *
 */
public class MessageModel {

	private MessageHeader header;
	private MessageBody body;
	private String host;
	
	public MessageModel(){}
	public MessageModel(MessageHeader header,MessageBody body){
		this.header = header;
		this.body = body;
	}
	public MessageHeader getHeader() {
		return header;
	}
	public void setHeader(MessageHeader header) {
		this.header = header;
	}
	public MessageBody getBody() {
		return body;
	}
	public void setBody(MessageBody body) {
		this.body = body;
	} 
	public int getMessageLength()
	{
		return this.header.getHeaderLength()+this.body.getBodyLength();
	}
	
    public static byte[] ObjectToByte(java.lang.Object obj) {  
        byte[] bytes = null;  
        try {  
            // object to bytearray  
            ByteArrayOutputStream bo = new ByteArrayOutputStream();  
            ObjectOutputStream oo = new ObjectOutputStream(bo);  
            oo.writeObject(obj);  
      
            bytes = bo.toByteArray();  
      
            bo.close();  
            oo.close();  
        } catch (Exception e) {  
            System.out.println("translation" + e.getMessage());  
            e.printStackTrace();  
        }  
        return bytes;  
    } 
    
    public void print() {
    	header.print();
    	body.print();
    }
	public String getHost() {
		return host;
	}
	public void setHost(String host) {
		this.host = host;
	}
}
